package net.sunofbeach.blog.services.impl;

import lombok.extern.slf4j.Slf4j;
import net.sunofbeach.blog.dao.PlanDao;
import net.sunofbeach.blog.pojo.Plan;
import net.sunofbeach.blog.response.ResponseResult;
import net.sunofbeach.blog.services.IPlanService;
import net.sunofbeach.blog.services.IUserService;
import net.sunofbeach.blog.utils.IdWorker;
import net.sunofbeach.blog.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class PlanServiceImpl extends BaseService implements IPlanService {
    @Autowired
    private IUserService userService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private PlanDao planDao;

    @Override
    public ResponseResult postPlan(Plan plan) {

        String content = plan.getContent();
        String state = plan.getState();
        Date createTime = plan.getCreateTime();
        if (TextUtils.isEmpty(content)) {
            return ResponseResult.FAILED("内容不可以为空");
        }

        if (TextUtils.isEmpty(state)) {
            return ResponseResult.FAILED("状态不可以为空");

        }

        if (TextUtils.isEmpty(createTime.toString())) {
            plan.setCreateTime(new Date());
        }else {
            plan.setCreateTime(createTime);
        }


        plan.setState(state);
        plan.setId(idWorker.nextId() + "");
        Date time = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatTime = sdf.format(time);
        plan.setTime(formatTime);
        plan.setCreateTime(time);
        planDao.save(plan);
        return ResponseResult.SUCCESS("计划提交成功");
    }

    @Override
    public ResponseResult listPlan(int page, int size) {
        page = checkPage(page);
        size = checkSize(size);
        //没有就往下走
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        Page<Plan> all =planDao.findAll(pageable);
        return ResponseResult.SUCCESS("获取计划列表成功").setData(all);
    }

    @Override
    public ResponseResult listPlanByTime( String time) {
        List<Plan> result = planDao.findAll(new Specification<Plan>() {
            @Override
            public Predicate toPredicate(Root<Plan> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                return criteriaBuilder.equal(root.get("time").as(String.class), time);
            }
        });
        return ResponseResult.SUCCESS("获取计划成功").setData(result);
}}

